﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB
{
    public class Utility
    {
        public static List<OLTS.MODEL.OTS_WORK_DETAIL> ConvertWorkDetailList(List<OLTS.MODEL.OTS_WORK_DETAIL> param)
        {
            var result = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
            foreach (var item in param)
            {
                var obj = new OLTS.MODEL.OTS_WORK_DETAIL();
                obj.AMOUNT_OF_TIME = item.AMOUNT_OF_TIME;
                obj.AMOUNT_OF_TIME_OVER_TIME = item.AMOUNT_OF_TIME_OVER_TIME;
                obj.DETAIL = item.DETAIL;
                obj.SEQ = item.SEQ;
                result.Add(obj);
            }

            return result;
        }

        public static int CheckInt(string param)
        {
            var result = 0;
            var i = int.TryParse(param, out result);

            return result; 
        }
        public static int CheckInt(int? param)
        {
            var result = 0; 
            var i = int.TryParse(param.ToString(), out result);

            return result;
        }

        public static DateTime? CheckNullableDate(string param)
        {
            if (param != string.Empty)
            {
                return Convert.ToDateTime(param);
            }
            else
            {
                return null;
            }
        }

        public static List<OTS_SITE> getSiteAll()
        {
            var cmd = new Site_Service();
            var result = cmd.GetAll();
            return result;

        }

        public static bool isHoliday(DateTime date)
        { 
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var site = BaseService.PROJECT.SITE_ID;
            var yr = DateTime.Now.Year.ToString();
            var cmd = new Holiday_Service(yr, Convert.ToInt16(site));
            var result = cmd.GetListHoliday();
            var isholiday = result.Where(x => x.DATE == date).FirstOrDefault();
            if (isholiday != null)
            {
                
                return true;
            }
            else
            {
                return false;
            }
        }


        private static readonly byte[] initBytes = Encoding.ASCII.GetBytes("aZ1489BnMpJmqwnz");
        private const string keypass = "cube9ebuc";
        private const int keysize = 256;

        public static string Encrypt(string enCrypText)
        {
            byte[] enCrypTextBytes = Encoding.UTF8.GetBytes(enCrypText);
            using (PasswordDeriveBytes sPDB = new PasswordDeriveBytes(keypass, null))
            {
                byte[] keyBytes = sPDB.GetBytes(keysize / 8);
                using (RijndaelManaged symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.Mode = CipherMode.CBC;
                    using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initBytes))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(enCrypTextBytes, 0, enCrypTextBytes.Length);
                                cryptoStream.FlushFinalBlock();
                                byte[] cipherTextBytes = memoryStream.ToArray();
                                return Convert.ToBase64String(cipherTextBytes);
                            }
                        }
                    }
                }
            }
        }

        public static string Decrypt(string deCrypText)
        {
            byte[] deCrypTextBytes = Convert.FromBase64String(deCrypText);
            using (PasswordDeriveBytes sPDB = new PasswordDeriveBytes(keypass, null))
            {
                byte[] keyBytes = sPDB.GetBytes(keysize / 8);
                using (RijndaelManaged symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.Mode = CipherMode.CBC;
                    using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initBytes))
                    {
                        using (MemoryStream memoryStream = new MemoryStream(deCrypTextBytes))
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                            {
                                byte[] plainTextBytes = new byte[deCrypTextBytes.Length];
                                int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                                return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                            }
                        }
                    }
                }
            }
        }
    }
}